package com.rahms.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rahms.pojo.entity.MedicationReminderLog;
import com.rahms.server.mapper.MedicationReminderLogMapper;
import com.rahms.server.service.MedicationReminderLogService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用药提醒记录服务实现类
 */
@Service
public class MedicationReminderLogServiceImpl extends ServiceImpl<MedicationReminderLogMapper, MedicationReminderLog> implements MedicationReminderLogService {
    
    @Override
    public List<MedicationReminderLog> getLogsByPlanId(Long planId) {
        LambdaQueryWrapper<MedicationReminderLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MedicationReminderLog::getPlanId, planId);
        return this.list(queryWrapper);
    }
    
    @Override
    public List<MedicationReminderLog> getLogsByElderlyId(Long elderlyId) {
        LambdaQueryWrapper<MedicationReminderLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MedicationReminderLog::getElderlyId, elderlyId);
        return this.list(queryWrapper);
    }
    
    @Override
    public List<MedicationReminderLog> getLogsByDateRange(Long elderlyId, LocalDateTime startDate, LocalDateTime endDate) {
        LambdaQueryWrapper<MedicationReminderLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MedicationReminderLog::getElderlyId, elderlyId)
                   .between(MedicationReminderLog::getScheduledTime, startDate, endDate);
        return this.list(queryWrapper);
    }
    
    @Override
    public boolean confirmMedication(Long logId, Long userId) {
        MedicationReminderLog log = this.getById(logId);
        if (log != null && log.getStatus() != 2) {  // 不是已确认状态
            log.setStatus(2);  // 设置为已确认
            log.setConfirmTime(LocalDateTime.now());
            log.setConfirmedBy(userId);
            return this.updateById(log);
        }
        return false;
    }
}